package hk.ust.android_competition.activities;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.google.gson.Gson;

import hk.ust.android_competition.pet.PetStatus;
import hk.ust.android_competition.store.Costume;
import hk.ust.android_competition.store.CostumeType;
import hk.ust.android_competition.store.Food;
import hk.ust.android_competition.store.PetProduct;
import hk.ust.android_competition.store.Shower;
import hk.ust.android_competition.view_adapter.MainViewAdapter;
import hk.ust.android_competition.data_reload_tools.TextWriter;
import hk.ust.android_competition.data_reload_tools.UserDataReloader;
import hk.ust.android_competition.e_pet.R;
import hk.ust.android_competition.pet.Pet;
import hk.ust.android_competition.user.UserData;


public class MainActivity extends LandscapeFullScreenActivity {
    //-----------------------Data-----------------------
    private String dataPath;
    private UserData ud;
    private Bundle extras;
    Thread t;
    MainViewAdapter viewAdapter;
    private MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        extras = getIntent().getExtras();
        dataPath = extras.getString("data path");
        player = new MediaPlayer();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (extras.getString("status").equals("new user")) {
            Pet pet = new Pet(extras.getString("pet name"), extras.getInt("pet type"));
//            try {
            ud = new UserData(extras.getString("user name"), pet, 10000);
//            } catch (IOException e) {
//                Toast.makeText(getApplicationContext(), "Internet not available, costume cannot update!", Toast.LENGTH_SHORT).show();
//            }
            extras.remove("status");
            saveData();
        } else {
            UserDataReloader reloader = new UserDataReloader(dataPath);
            ud = reloader.getUserData();
        }
        viewAdapter = new MainViewAdapter(this, R.layout.activity_main, ud);
        viewAdapter.updateAll(ud);
        t = new Thread() {
            @Override
            public void run() {
                try {
                    while (!isInterrupted()) {
                        Thread.sleep(100);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ud.getPet().timePass(0.006);
                                ud.coinIncreasePerTime();
                                viewAdapter.update(ud);
                            }
                        });

                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();
    }

    @Override
    public void onPause() {
        super.onPause();
        player.pause();
        saveData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        player.stop();
        player.release();
    }

    private void saveData() {
        Gson gson = new Gson();
        String dataToJSON = gson.toJson(ud);
        TextWriter writer = new TextWriter(dataPath);
        writer.write(dataToJSON);
    }

    int storeItemType = 0;
    int eventItemType = 0;
    PetProduct purchaseProduct;

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.lv_store://Store list
                switch (position) {
                    case 0: //food
                        storeItemType = 1;
                        viewAdapter.updateListView(R.id.lv_store, 0);
                        break;
                    case 1: //costume
                        storeItemType = 2;
                        viewAdapter.updateListView(R.id.lv_store, 1);
                        break;
                    case 2: //clean
                        storeItemType = 3;
                        viewAdapter.updateListView(R.id.lv_store, 2);
                        break;
                }
                this.state = MainActivityState.ListShown;
                break;
            case R.id.lv_event://Event list
                switch (position) {
                    case 0: //talk
                        eventItemType = 1;
                        viewAdapter.updateListView(R.id.lv_event, 0);
                        this.state = MainActivityState.ListShown;
                        break;
                    case 1: //play
                        eventItemType = 2;
                        Intent game = new Intent(this, MiniGameActivity.class);
                        game.putExtra("pet type", ud.getPet().getType().getValue());
                        this.startActivity(game);
                        ud.getPet().play();
                        this.state = MainActivityState.Standby;
                        break;
                    case 2: //sing
                        eventItemType = 3;
                        viewAdapter.updateListView(R.id.lv_event, 2);
                        ud.getPet().listenASong();
                        this.state = MainActivityState.Standby;
                        break;
                }

                break;
            case R.id.lv_event_lv:
                switch (eventItemType) {
                    case 1: //Talk list
                        switch (position) {
                            case 0://How are you?
                                viewAdapter.showMessageBubble(ud.getPet().showStatus());
                                break;
                            case 1://What is your name
                                viewAdapter.showMessageBubble("My name is " + ud.getPet().getPetName());
                                break;
                            case 2://Do you want to bath?
                                if (!ud.getPet().isClean())
                                    viewAdapter.showMessageBubble("Yes!");
                                else
                                    viewAdapter.showMessageBubble("No, thank you!");
                                break;
                            case 3://Do you want to sleep?
                                if(ud.getPet().isSleepy())
                                    viewAdapter.showMessageBubble("Yes! I want to a song.");
                                else
                                    viewAdapter.showMessageBubble("No, thank you!");
                                break;
                        }
                        break;
                    case 3://Talk sing
                        switch (position) {
                            case 0:
                                player.stop();
                                player.release();
                                player = MediaPlayer.create(this, R.raw.london);
                                viewAdapter.setPet_status(PetStatus.Sleep);
                                break;
                            case 1:
                                player.stop();
                                player.release();
                                player = MediaPlayer.create(this, R.raw.letitgo);
                                viewAdapter.setPet_status(PetStatus.Sleep);
                                ud.getPet().smile();
                                break;
                        }
                        ud.getPet().listenASong();
                        viewAdapter.resetAllListView();
                        player.start();
                }
                this.state = MainActivityState.Standby;
                break;
            case R.id.lv_store_lv:
                switch (storeItemType) {
                    case 1:
                        switch (position) {
                            case 0: // Apple
                                purchaseProduct = new Food("apple", 10, 10, 30, 0);
                                break;
                            case 1: // Banana
                                purchaseProduct = new Food("banana", 20, 10, 40, 0);
                                break;
                            case 2: // Pear
                                purchaseProduct = new Food("pear", 15, 20, 50, 0);
                                break;
                            case 3: // cake
                                purchaseProduct = new Food("cake", 25, 35, 80, 0);
                                break;
                            case 4: // burger
                                purchaseProduct = new Food("burger", 35, 40, 100, 0);
                                break;
                            case 5: // hotpot
                                purchaseProduct = new Food("hotpot", 45, 50, 150, 0);
                                break;
                            case 6: // medicine
                                purchaseProduct = new Food("medicine", 0, 20, 150, 200);
                                break;
                        }
                        viewAdapter.showMessageBox("Pay " + purchaseProduct.getCost() + " coins for " + purchaseProduct.getName() + "?");
                        this.state = MainActivityState.MessageBoxShown;
                        break;
                    case 2:
                        switch (position) {
                            case 0:
                                purchaseProduct = new Costume("Pilot", 2000, 20, CostumeType.Pilot);
                                break;
                            case 1:
                                purchaseProduct = new Costume("Student", 4000, 50, CostumeType.Student);
                                break;
                        }
                        if (ud.getCostumeCategory().getCostumeAvailable()[position]) {
                            ud.getPet().wearCostume((Costume) purchaseProduct);
                            viewAdapter.updateCostume(ud);
                            viewAdapter.resetAllListView();
                        } else {
                            viewAdapter.showMessageBox("Pay " + purchaseProduct.getCost() + " coins for the costume " + purchaseProduct.getName() + "?");
                            this.state = MainActivityState.MessageBoxShown;
                        }
                        break;
                    case 3:
                        switch (position) {
                            case 0: // Shower
                                purchaseProduct = new Shower("Shower", 50);
                                viewAdapter.showMessageBox("Pay " + purchaseProduct.getCost() + " coins for shower?");
                                this.state = MainActivityState.MessageBoxShown;
                                break;
                        }
                        break;
                }
                storeItemType = 0;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.show_status:
                Toast.makeText(this, ud.getPet().showStatus(), Toast.LENGTH_LONG).show();
                break;
            case R.id.time_pass:
                ud.getPet().timePass(1);
                ud.coinIncreasePerTimePass();
                break;
            case R.id.iv_pet_main:
                viewAdapter.petImageIsTouched();
                ud.getPet().smile();
                break;
            case R.id.reset:
                ud.getPet().reset();
                break;
            case R.id.open_store_button:
                viewAdapter.sideButtonIsTouched(R.id.open_store_button);
                this.state = MainActivityState.ListShown;
                break;
            case R.id.open_event_button:
                viewAdapter.sideButtonIsTouched(R.id.open_event_button);
                this.state = MainActivityState.ListShown;
                break;
            case R.id.button_yes:
                if (ud.buyProduct(purchaseProduct)) {
                    switch (purchaseProduct.getType()) {
                        case Food:
                            ud.getPet().eat((Food) purchaseProduct);
                            break;
                        case Costume:
                            ud.makeCostumeAvailable((Costume) purchaseProduct);
                            ud.getPet().wearCostume((Costume) purchaseProduct);
                            viewAdapter.updateCostume(ud);
                            break;
                        case Shower:
                            ud.getPet().shower((Shower) purchaseProduct);
                            break;
                        default:
                            break;
                    }
                    viewAdapter.hideMessageBox();
                    viewAdapter.resetAllListView();
                    this.state = MainActivityState.Standby;
                } else
                    Toast.makeText(getApplicationContext(), "Not enough coins!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.button_no:
                viewAdapter.hideMessageBox();
                this.state = MainActivityState.ListShown;
                break;
            default:
                break;
        }
    }

    private MainActivityState state = MainActivityState.Standby;

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        switch (state) {
            case MessageBoxShown:
                viewAdapter.hideMessageBox();
                break;
            case ListShown:
                viewAdapter.resetAllListView();
                break;
            case Standby:
                finish();
                break;
        }
    }

}

